package com.tongtong.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @Title: FileTImerTask
 * @Author zl
 * @Package com.tongtong.task
 * @Date 2025/7/11 9:42
 * @description: 文件定时任务
 */
@Slf4j
@Component
public class MumudaTimerTask {
    @Value("${upload.filePath}")
    private String filePath;

    public void checkFile(){
        File directory = new File(filePath);
        if (!directory.exists()) {
            log.warn("文件路径不存在");
            return;
        }

        File[] files = directory.listFiles();
        if (files == null) {
            log.warn("无法读取文件列表");
            return;
        }

        long currentTime = System.currentTimeMillis();
        long thirtyMinutesInMillis = 30 * 60 * 1000;

        for (File file : files) {
            if (file.isFile()) {
                long lastModifiedTime = file.lastModified();
                if (currentTime - lastModifiedTime > thirtyMinutesInMillis) {
                    boolean deleted = file.delete();
                    if (deleted) {
                        log.warn("已删除超过30分钟的文件：" + file.getName());
                    } else {
                        log.warn("删除文件失败：" + file.getName());
                    }
                }
            }
        }
    }
}
